const htmlMinTask = () => {
	return {
		config: {
			dist: {
				options: {
					removeComments: true,
					collapseWhitespace: true
				},
				files: [{
					expand: true,
					cwd: 'script_src/html',
					src: ['**/*.html'],
					dest: 'bulid'
				}]
			}
		},
		plugins: "grunt-contrib-htmlmin"
	};
};
const uglifyTask = () => {
	return {
		config: {
			target: {
				files: [
					{
						expand: true,
						cwd: 'script_src/html',
						src: ['**/*.js'],
						dest: 'bulid'
					}
				]
			}
		},
		plugins: "grunt-contrib-uglify"
	};
};
const copyTask = () => {
	return {
		config: {
			static: {
				expand: true,
				cwd: 'script_src/static',
				src: '**',
				dest: 'bulid/static',
			},
		},
		plugins: "grunt-contrib-copy"
	};
};
const browerSyncTask = () => {
	return {
		config: {
			dev: {
				bsFiles: {
					src: [
						'bulid/**/*'
					]
				},
				options: {
					watchTask: true,
					server: './bulid',
					browser: "google chrome",
					startPath: 'webstorage/index.html',
					ui: false,
					port: 9001,
				}
			}
		},
		plugins: "grunt-browser-sync",
		task: "browserSync",
	};
}
const watchTask = () => {
	return {
		config: {
			scripts: {
				files: ["script_src/html/**/*.js"],
				tasks: ["uglify"],
				options: {
					reload: true
				}
			},
			html: {
				files: ["script_src/html/**/*.html"],
				tasks: ["htmlmin"],
				options: {
					reload: true
				}
			}
		},
		plugins: "grunt-contrib-watch"
	};
};
const cleanTask = () => {
	return {
		config: {
			folder: ['bulid/']
		},
		plugins: 'grunt-contrib-clean'
	}
};
// 根据插件名称获取任务  最后一个-的后面部分
const getGruntTask = (plugins) => {
	let p = plugins.split('-');
	return p[p.length - 1];
};
module.exports = (grunt) => {
	let tasks = [cleanTask(), htmlMinTask(), uglifyTask(), copyTask(), browerSyncTask(), watchTask()],
		initConfig = {},
		loadNpmTasks = []
	tasks.forEach(task => {
		Object.assign(initConfig, {
			[task.task ? task.task : getGruntTask(task.plugins)]: task.config
		});
		loadNpmTasks.push(task.plugins);
	})
	grunt.initConfig(initConfig);
	loadNpmTasks.forEach(task => {
		grunt.loadNpmTasks(task)
	})
	grunt.registerTask('default', Object.keys(initConfig))
};
